﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using Meqs.Entity;
using Meqs.Logic.Interface;
using System.Collections.Generic;
using Castle.Windsor;

public partial class Education_EducationUserGrid : System.Web.UI.Page
{
    private static readonly IEduPremLogic _eduPrmLogic = Container<IEduPremLogic>.Instance;

    private List<PremInfo> _listPrems = new List<PremInfo>();
    private List<EduPrem> _listEduPrems = new List<EduPrem>();

    //IEduPremLogic eduPremLogic = Container<IEduPremLogic>.Instance;

    protected void Page_Load(object sender, EventArgs e)
    {
        Hashtable htPrem = new Hashtable();
        htPrem.Add("PremType", 1);
        _listPrems = (List<PremInfo>)Container<IPremInfoLogic>.Instance.FindAll(htPrem);


        _listEduPrems = (List<EduPrem>)Container<IEduPremLogic>.Instance.FindAll();

    }

    protected void btnBind_Click(object sender, EventArgs e)
    {
        Bind(1);
    }

    private void Bind(int pageIndex)
    {
        string iSchoolArea = HFSchoolAreaValue.Value;
        //获取查询条件类型和值
        int qType = int.Parse(HFQueryType.Value);
        string qValue = HFQueryValue.Value;

        Hashtable ht = new Hashtable();
        ht.Add("AreaID", iSchoolArea);
        ht.Add("IsAdmin", 0);

        IEducationUsersLogic userLogic = Container<IEducationUsersLogic>.Instance;
        List<EducationUsers> list = new List<EducationUsers>();
        switch (qType)
        {//0: 查询所有
            case 0:
                //根据条件查询数据库
                list = (List<EducationUsers>)userLogic.FindAll(ht);

                //分页处理
                PagerBind(list, pageIndex);

                break;
            //1: 按用户名编号查询
            case 1:
                //根据条件查询数据库
                ht.Add("UserName", qValue);
                list = (List<EducationUsers>)userLogic.GetByCondition(ht);
                //分页处理
                PagerBind(list, pageIndex);

                break;
            //2: 按用户实名查询
            case 2:
                //根据条件查询数据库
                ht.Add("UserActualName", qValue);
                list = (List<EducationUsers>)userLogic.GetByCondition(ht);
                //分页处理
                PagerBind(list, pageIndex);

                break;
            default:
                break;

        }       

    }

    //分页处理
    private void PagerBind(List<EducationUsers> srcList, int pageIndex)
    {
        AspNetPager1.RecordCount = srcList.Count;
        AspNetPager1.CurrentPageIndex = pageIndex;

        int index = (pageIndex - 1) * AspNetPager1.PageSize;
        int count = 0;
        if (srcList.Count - index >= AspNetPager1.PageSize) count = AspNetPager1.PageSize;
        else count = srcList.Count - index;

        List<EducationUsers> pageList = srcList.GetRange(index, count);

        Repeater1.DataSource = pageList;
        Repeater1.DataBind();
    }

    protected void AspNetPager1_PageChanged(object src, EventArgs e)
    {
        Wuqi.Webdiyer.PageChangedEventArgs pc = e as Wuqi.Webdiyer.PageChangedEventArgs;

        Bind(pc.NewPageIndex);
    }


    /// <summary>
    /// 根据主键来获取该用户的信息
    /// </summary>
    /// <param name="RID"></param>
    /// <returns></returns>
    [System.Web.Services.WebMethod]
    public static EducationUsers SelectUser(int RID)
    {
        EducationUsers user = Container<IEducationUsersLogic>.Instance.FindById(RID);
        return user;
    }

    /// <summary>
    /// 获取用户权限
    /// </summary>
    /// <param name="RID"></param>
    /// <returns></returns>
    [System.Web.Services.WebMethod]
    public static List<int> GetUserRight(int RID)
    {
        Hashtable ht = new Hashtable();
        ht.Add("EduUserID", RID);
        List<EduPrem> eduPrems = (List<EduPrem>)Container<IEduPremLogic>.Instance.FindAll(ht);
        List<int> result = new List<int>();
        foreach(EduPrem ed in eduPrems) 
        {
            result.Add(ed.PNo);
        }

        return result;
    }

    /// <summary>
    /// 插入一个用户，绑定对应的权限
    /// </summary>
    /// <param name="ed"></param>
    /// <param name="iRights"></param>
    /// <returns></returns>
    [System.Web.Services.WebMethod]
    public static int InsertUser(EducationUsers ed, List<int> iRights)
    {
        ed.Passwd = Common.GetStartPwd();

        //IEducationUsersLogic userLogic = Container<IEducationUsersLogic>.Instance;
        //IEduPremLogic eduPremLogic = Container<IEduPremLogic>.Instance;

        //int iResult = userLogic.Create(ed);

        //if (iResult > 0)
        //{
        //    foreach (int Right in iRights)
        //    {
        //        EduPrem edu = new EduPrem(iResult, 1, Right);

        //        eduPremLogic.InsertUser(edu);
        //    }

        //    return 1;
        //}
        //else
        //    return -1;
        //----------------------------------

        List<EduPrem> eduPrms = new List<EduPrem>();
        foreach (int Right in iRights)
        {
            EduPrem edu = new EduPrem(ed.RID, Right);
            eduPrms.Add(edu);
        }
        IEducationUsersLogic userLogic = Container<IEducationUsersLogic>.Instance;

        return userLogic.InsertUser(ed, eduPrms);
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="ed"></param>
    /// <param name="iRights"></param>
    /// <returns></returns>
    [System.Web.Services.WebMethod]
    public static int UpdateUser(EducationUsers ed, List<int> iRights)
    {
        //IEducationUsersLogic userLogic = Container<IEducationUsersLogic>.Instance;
        // IEduPremLogic eduPremLogic = Container<IEduPremLogic>.Instance;
        //int iResult = userLogic.UpdateUser(ed);

        //if (iResult > 0)
        //{
        //    eduPremLogic.Delete(ed.RID);

        //    foreach (int Right in iRights)
        //    {
        //        EduPrem edu = new EduPrem(ed.RID, 1, Right);

        //        eduPremLogic.InsertUser(edu);
        //    }

        //    return 1;
        //}
        //else
        //    return -1;
        //----------------------------------------------------

        List<EduPrem> eduPrms = new List<EduPrem>();
        foreach (int Right in iRights)
        {
            EduPrem edu = new EduPrem(ed.RID, Right);
            eduPrms.Add(edu);
        }

        IEducationUsersLogic userLogic = Container<IEducationUsersLogic>.Instance;
        return userLogic.UpdateUser(ed, eduPrms);
    }

    /// <summary>
    /// 验证用户是否已经存在
    /// </summary>
    /// <param name="ed"></param>
    /// <returns></returns>
    [System.Web.Services.WebMethod]
    public static int ValidateUserInfo(EducationUsers ed)
    {
        int result = 0;
        IEducationUsersLogic userLogic = Container<IEducationUsersLogic>.Instance;

        //1.新增
        if (ed.RID == 0)
        {
            Hashtable tableName = new Hashtable();
            tableName.Add("UserName", ed.UserName);
            tableName.Add("AreaID", ed.AreaID);
            result = userLogic.FindCount(tableName);

            //学校编号重复
            if (result > 0) return 1;
        }
        //2.修改
        else
        {
            EducationUsers eduUser = userLogic.FindById(ed.RID);
            //修改了工号

            if (eduUser.UserName.ToUpper() != ed.UserName.ToUpper())
            {
                Hashtable tableName = new Hashtable();
                tableName.Add("UserName", ed.UserName);
                tableName.Add("AreaID", ed.AreaID);
                result = userLogic.FindCount(tableName);

                //学校编号重复
                if (result > 0) return 1;
            }
        }

        return result;
    }

    /// <summary>
    /// 重新设置密码
    /// </summary>
    /// <param name="users"></param>
    /// <returns></returns>
    [System.Web.Services.WebMethod]
    public static int ResetUserPwd(List<int> users)
    {
        string strStartPwd = Common.GetStartPwd();

        int n = Container<IEducationUsersLogic>.Instance.ResetPwd(strStartPwd, users);

        return n;
    }

    /// <summary>
    /// 删除用户
    /// </summary>
    /// <param name="users"></param>
    /// <returns></returns>
    [System.Web.Services.WebMethod]
    public static int DeleteUsers(List<int> users)
    {
        IEducationUsersLogic userLogic = Container<IEducationUsersLogic>.Instance;
        return userLogic.DeleteUser(users);
    }

    /*
    
    /// <summary>
    /// 获取指定的学区的所有用户信息
    /// </summary>
    /// <param name="iSchoolArea"></param>
    /// <returns></returns>
    //[System.Web.Services.WebMethod]
    //public static int BindDataByAreaID(int iSchoolArea)
    //{
    //    Hashtable ht = new Hashtable();
    //    ht.Add("AreaID", iSchoolArea);
    //    ht.Add("IsAdmin", 0);

    //    IEducationUsersLogic userLogic = Container<IEducationUsersLogic>.Instance;

    //    List<EducationUsers> list = (List<EducationUsers>)userLogic.FindAll(ht);

    //    viewData = list;

    //    return 0;
    //}
    
    /// <summary>
    /// 根据用户名来查询用户
    /// </summary>
    /// <param name="userName"></param>
    /// <returns></returns>
    [System.Web.Services.WebMethod]
    public static int BindDataByUserName(int iSchoolArea, string userName)
    {
        Hashtable table = new Hashtable();
        table.Add("UserName", userName);
        table.Add("IsAdmin", 0);
        table.Add("AreaID", iSchoolArea);

        IEducationUsersLogic userLogic = Container<IEducationUsersLogic>.Instance;
        List<EducationUsers> list = (List<EducationUsers>)userLogic.GetByCondition(table);

        viewData = list;
        return 1;
    }

    /// <summary>
    /// 根据用户真实姓名来查询
    /// </summary>
    /// <param name="realName"></param>
    /// <returns></returns>
    [System.Web.Services.WebMethod]
    public static int BindDataByRealName(int iSchoolArea, string realName)
    {
        Hashtable table = new Hashtable();
        table.Add("UserActualName", realName);
        table.Add("IsAdmin", 0);
        table.Add("AreaID", iSchoolArea);

        IEducationUsersLogic userLogic = Container<IEducationUsersLogic>.Instance;
        List<EducationUsers> list = (List<EducationUsers>)userLogic.GetByCondition(table);

        viewData = list;
        return 1;
    }
    */

    /// <summary>
    /// 根据用户真实姓名来查询
    /// </summary>
    /// <param name="realName"></param>
    /// <returns></returns>
    [System.Web.Services.WebMethod]
    public static List<SystemPrem> GetUserSystemPrm(int userSystemPrem)
    {
        List<SystemInfo> listSystem = (List<SystemInfo>)Container<ISystemInfoLogic>.Instance.GetSystemPremByType(userSystemPrem);

        List<SystemPrem> listResult = new List<SystemPrem>();

        foreach(SystemInfo systemItem in listSystem)
        {
            Hashtable htPrem = new Hashtable();
            htPrem.Add("SystemNo", systemItem.SystemNo);
            List<PremInfo> listprem = (List<PremInfo>)Container<IPremInfoLogic>.Instance.FindAll(htPrem);

            SystemPrem sysPrem = new SystemPrem(systemItem.RID, systemItem.SystemNo, systemItem.SystemName, systemItem.Remark);

            List<PremInfo> premList = new List<PremInfo>();

            foreach(PremInfo premItem in listprem)
            {
                premList.Add(premItem);
            }

            sysPrem.PremList = premList;

            listResult.Add(sysPrem);
        }

        return listResult;
    }

    public string GetPremHeaderString(int premType)
    {
        string strTitle = "";

        foreach (PremInfo premItem in _listPrems)
        {
            strTitle += "<th width=\"17%\" style=\"text-align: left;padding-left:5px;\">";
            strTitle += premItem.PremName;
            strTitle += "</th>";
        }

        return strTitle;
    }


    public string GetUserPrem(int iRID)
    {
        string strCell = "";
        foreach (PremInfo premItem in _listPrems)
        {
            int iRight = 0;
            strCell += "<td style=\"text-align: center;\">";
            foreach (EduPrem eduPremItem in _listEduPrems)
            {
                if (premItem.PremNo == eduPremItem.PNo && eduPremItem.EduUserID == iRID)
                {
                    strCell += "<img src = '../common/images/right.gif'>";
                    iRight = 1;
                    break;
                }
            }

            if (iRight == 0)
            {
                //strCell += "<img src = '../common/images/wrong.gif'>";
            }

            strCell += "</td>";
        }

        return strCell;
    }


}
